/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.netbrain.dao.impl;

import java.util.List;
import mx.netbrain.dao.TreeDao;
import mx.netbrain.model.Status;
import mx.netbrain.model.Tree;
import mx.netbrain.model.User;
import mx.netbrain.util.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author Julian
 */
public class TreeDaoImpl implements TreeDao {

    @Override
    public void save(Tree tree) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Status status = new Status();
        status.setId(1);
        tree.setStatus(status);
        try {
            
            session.beginTransaction();
            session.persist(tree);
            session.beginTransaction().commit();
        } catch (Exception e) {
            session.beginTransaction().rollback();
        }
    }

    @Override
    public void update(Tree tree) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.merge(tree);
            session.beginTransaction().commit();
        } catch (Exception e) {
            session.beginTransaction().rollback();
        }
    }

    @Override
    public Tree findById(Long id) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        return (Tree) session.load(Tree.class, id);
    }

    @Override
    public List<Tree> listAll(User user) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        String sql = "select t from Tree t join t.treeUsers u where u.user.id=:user and "
                + "t.root=0 and t.status.id=1";
        Query query = session.createQuery(sql);
        query.setLong("user", user.getId());
        return query.list();
    }

    @Override
    public void delete(Tree tree) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<Tree> listRoot(User user) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        String sql = "select t from Tree t join t.treeUsers u where u.user.id=:user and "
                + "t.type=:type and t.status.id=1";
        Query query = session.createQuery(sql);
        query.setLong("user", user.getId());
        query.setString("type", "Carpeta");
        return query.list();
    }

    @Override
    public List<Tree> listNodeId(Long id, User user) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        String sql = "select t from Tree t join t.treeUsers u where t.root=:root and t.type=:type "
                + "and u.user.id=:user and t.status.id=1";
        Query query = session.createQuery(sql);
        query.setLong("root", id);
        query.setString("type", "Carpeta");
        query.setLong("user", user.getId());
        return query.list();
    }

    @Override
    public List<Tree> listNodeFolder(Long id, User user) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        String sql = "select t from Tree t join t.treeUsers u where t.root=:root and "
                + "u.user.id=:user and t.status.id=1";
        Query query = session.createQuery(sql);
        query.setLong("root", id);
        query.setLong("user", user.getId());
        return query.list();
    }
}
